require('./PageChangereq.styl');
import { NEW, RUNNING, TERMINATED, COMPLETED, CANCELED } from 'clientConfig/constants'
const reactMixin = require('react-mixin');
const Actions = require('./actions');
const Store = require('./store');
const { Group, TextField, SelectField, PhotoField, Icon, Button, TextareaField, DatetimeField, Boxs, Mask } = SaltUI;
const { HBox, Box } = Boxs;
import Addpersonfield from 'components/addpersonfield'
import PageTenderplan from 'pages/home/subpages/tender/subpages/tenderplan';
import PageProcure from 'pages/home/subpages/tender/subpages/procure';
import PagePurchase from 'pages/home/subpages/tender/subpages/purchase';
import PageContractsign from 'pages/home/subpages/tender/subpages/contractsign';
import PageSoftware from 'pages/home/subpages/workplan/subpages/software';
import PageSoucecode from 'pages/home/subpages/achievements/subpages/achievementmanage';
import PagePayPlan from 'pages/home/subpages/paymanage/pagesubs/payplan';
import PagePayReq from 'pages/home/subpages/paymanage/pagesubs/paymanagesub';
import PagePayTransfer from 'pages/home/subpages/paymanage/pagesubs/paytransfer';
import PageFaultReq from 'pages/home/subpages/maintain/subpages/faultreport';
import PageFaulthandle from 'pages/home/subpages/maintain/subpages/faulthandle';
import PageFaultarrange from 'pages/home/subpages/maintain/subpages/faultarrange';
import PageFaultfeedback from 'pages/home/subpages/maintain/subpages/faultfeedback';
const classNames = require("classnames")

let components = {
    tenderplan: PageTenderplan,
    tenderprocure: PageProcure,
    tenderpurchase: PagePurchase,
    contractsign: PageContractsign,
    software: PageSoftware,
    sourcecode: PageSoucecode,
    payplan: PagePayPlan,
    payreq: PagePayReq,
    faultreq: PageFaultReq,
    faulthandle: PageFaulthandle,
    faultarrange: PageFaultarrange,
    faultfeedback: PageFaultfeedback,
    paytransfer: PagePayTransfer
}
const ChangereqContent = (props) => {
    const SpecificStory = components[props.type];
    return SpecificStory ? <SpecificStory pagedata={props.pagedata} edit={props.edit} /> : <noscript></noscript>;
}
const options = [
    { value: 0, text: '内容变更' },
    { value: 1, text: '撤销申请' }
]
class Changereq extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            approvalType: "",
            options: options,
            // maskvisible: false,
            edit: true//是否为撤销false：撤销 true：内容变更
        };
        this.isShow = { display: 'block' }
        this.strName = ""
        this.changeType = null
        this.changeTypeName = null
    }
    handleTextChange(name, newValue) {
        //0为内容变更1为撤销
        if (name == 'changeType') {
            this.changeType = newValue.value
            if (newValue.value == 1) {
                this.isShow.display = 'none'
            } else {
                this.isShow.display = 'block'
            }
            this.setState({ edit: !newValue.value })
        } else if (name == 'beforeChange') {
            this.strName = this.state.content[newValue.value].name
        } else if (name == 'processNum') {
            if (newValue.length == 36) {
                let cb = () => {
                    if (this.state.pagedata.status == NEW || this.state.pagedata.status == RUNNING) {
                        this.state.pagedata.changeType = '撤销申请';
                        this.setState({
                            pagedata: this.state.pagedata,
                            options: [options[1]],
                            "changeType": { value: 1, text: '撤销申请' },
                            edit: false,
                        }, console.log(this.state.pagedata));
                    } else {
                        this.setState({
                            "changeType": { value: 0, text: '内容变更' }, edit: true
                        }, () => {
                            console.log(this.state.edit)
                        });
                    }
                }
                Actions.fetch(newValue, cb)
            }
        }
        this.setState({
            [name]: newValue
        });
    }

    upFile() {
        this.fileName.push(this.refs.fileInput.value)
        this.setState({ upfileName: this.fileName })
        var fileData = new FormData(this.refs.fileInput);
    }

    render() {
        let t = this;
        return (
            <div className="changereq myform">
                <Group.List>
                    <SelectField
                        label="变更类型"
                        placeholder="请选择（必填）"
                        options={t.state.options}
                        className="changeType"
                        value={t.state.changeType}
                        onSelect={t.handleTextChange.bind(t, 'changeType')}
                    />
                </Group.List>
                <Group.List>
                    <TextField
                        label="审批编号"
                        placeholder="请黏贴或者输入已审批通过的审批编号"
                        value={t.state.processNum}
                        onChange={t.handleTextChange.bind(t, 'processNum')}

                    />
                </Group.List>
                <ChangereqContent type={t.state.approvalType} pagedata={t.state.pagedata} edit={!t.state.edit} />
            </div>
        );
    }

    componentWillMount() {
        if (this.state.approvalType) {
            this.setState({ approvalType: '' })
        }
    }

    componentDidMount() {

    }

    componentWillReceiveProps(nextProps) {
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {

    }

    componentWillUnmount() {

    }
}

reactMixin.onClass(Changereq, Reflux.connect(Store));

module.exports = Changereq;
